//index.js
//获取应用实例
const app = getApp();
const util = require('../../utils/util.js');
const TAG = 'index.js';
var time_fetchRoomList;
var requestRoomListUrl = "https://liveroom1739272706-api.zego.im/demo/roomlist?appid=1739272706";

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
 
    userID:'',
    roomName: '',
    roomID:'',
    roomList: [],
    userName: '',
    tapTime: '',
    loginType: '', // 主播：anchor；观众：audience

  },
  
  onLoad: function () {
    util.logcatLiveCycle(TAG,"onLoad");
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    util.logcatLiveCycle(TAG,"onShow");
    var self = this;
    //1.请求房间列表。
    this.fetchRoomList(self);
    // time_fetchRoomList = setInterval(this.fetchRoomList(self),2000);
    //2.加入聊天大厅。
    this.enterChatHall();
  },

  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    util.logcatLiveCycle(TAG,"onHide");
    //1.清除列表查询定时器
    if(time_fetchRoomList){
      clearTimeout(time_fetchRoomList);
    };
    //2.退出聊天室
    this.outChatHall();
  },

  onReady:function(){
    util.logcatLiveCycle(TAG,"onReady");
    if(app.globalData.userID){
    }else{
      wx.showToast({
        title: '用户ID获取失败，尝试重新打开应用获取信息',
        icon: 'fail',
        duration: 1000,
        mask:true
      })
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    util.logcatLiveCycle(TAG,"onUnload");
    this.stopRefresh(self);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    util.logcatLiveCycle(TAG,"onPullDownRefresh");
    var self = this;
    this.fetchRoomList(self);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    util.logcatLiveCycle(TAG,"onReachBottom");
  },
  enterChatHall:function () {

  },
  outChatHall:function () {

  },
  fetchRoomList:function (self) {
    console.log('fetchRoomList');
    if(time_fetchRoomList){
      clearTimeout(time_fetchRoomList);
    };
    // wx.showLoading({
    //   title: '获取房间列表'
    // });
    wx.request({
      url: requestRoomListUrl,
      method: "GET",
      success: function (res) {
        self.stopRefresh();
        console.log("fetchRoomList, result is: ");
        if (res.statusCode === 200) {
          console.log(res.data);
          self.setData({
            roomList: res.data.data.room_list
          })
        } else {
          wx.showToast({
            title: '获取房间列表失败，请稍后重试',
            icon: 'none',
            duration: 2000
          })
        }
      },
      complete:function(){
        time_fetchRoomList = setTimeout(function(){
          self.fetchRoomList(self)
        },5000);
      }
    })
  },
  stopRefresh: function () {
    wx.hideLoading();
    wx.stopPullDownRefresh();
  },
  createRoom:function () {
    var url = '../createroom/createroom';
    wx.navigateTo({
      url: url,
    });
  }
})
